import csv
import matplotlib.pyplot as plt
from datetime import datetime


def switch(numbers):
    '''华氏度转化为摄氏度'''
    c = []
    for number in numbers:
        c_number = (int(number) - 32) * 5//9
        c.append(c_number)
    return c

filename = 'death_valley_2014.csv'
with open(filename) as file_obj:
    '''读取文件首行'''
    reader = csv.reader(file_obj)
    head_row = next(reader)

    '''获取最高最低温度（华氏度）'''
    highs, lows, dates = [], [], []
    for reader_simple in reader:
        try:
            date_simple = datetime.strptime(reader_simple[0], "%Y-%m-%d")
            high = int(reader_simple[1])
            low = int(reader_simple[3])
        except ValueError:
            # pass
            print(date_simple, 'miss')
        else:
            dates.append(date_simple)
            highs.append(high)
            lows.append(low)
    # print(dates)

highs_c = highs#switch(highs[:])
lows_c = lows#switch(lows[:])
# print(highs_c, lows_c)

#绘制图表
fig = plt.figure(dpi=128, figsize=(16, 9))
plt.plot(dates, highs_c, c='red', alpha=0.5)
plt.plot(dates, lows_c, c='green', alpha=0.5)

#中间填充颜色 
plt.fill_between(dates, highs_c, lows_c, facecolor='blue', alpha=0.1)

#设置图标格式 
title = "Daily high and low temperatures - 2014\nDeath Valley, CA"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
